package de.snom;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gdata.client.Query;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.FamilyName;
import com.google.gdata.data.extensions.GivenName;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class AdressBook {

	private static AdressBook instance;
	private List<Contact> contacts;
	private String username;
	private String password;

	public static AdressBook get() {
		if (instance == null) {
			instance = new AdressBook();
		}
		return instance;
	}

	private AdressBook() {
		contacts =  new ArrayList<Contact>();
	}

	public List<Contact> getContactsForString(String string) {
		
		ArrayList<Contact> sublist =  new ArrayList<Contact>();
				
				for (Contact contact : contacts) {
					try {
						if (contact.getName().substring(0, string.length())
								.equalsIgnoreCase(string)
								|| contact.getSurname().substring(0, string.length())
								.equalsIgnoreCase(string)) {
							sublist.add(contact);
						}
					} catch (StringIndexOutOfBoundsException e) {
						// do nothing
					}
				}
		
		return sublist ;
	}
	
	public boolean updateAdressBook() {
		ContactsService contactsService =  new ContactsService("CTI_Tool");
				
		try {
			contactsService.setUserCredentials(username, password);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		 URL feedUrl;
		try {
			feedUrl = new URL("https://www.google.com/m8/feeds/contacts/"+username+"/full");
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
			return false;
		}
		  Query  query =  new Query(feedUrl);
		  query.setMaxResults(2000);
		  
		  ContactFeed resultFeed;
		try {
			resultFeed = contactsService.getFeed(query, ContactFeed.class);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		  
		  // Print the results
		  this.contacts =  new ArrayList<Contact>();
		  for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		    ContactEntry entry = resultFeed.getEntries().get(i);
		    Name name = entry.getName();
			if (name != null) {
				List<PhoneNumber> phoneNumbers = entry.getPhoneNumbers();
				if (phoneNumbers.size() > 0) {
					FamilyName familyName = name.getFamilyName();
					GivenName givenName = name.getGivenName();
					if ((familyName != null && !"".equals(familyName))||(givenName != null && !"".equals(givenName))) {
						Contact contact = new Contact();
						if (name.getFamilyName() != null)
							contact.setName(name.getFamilyName().getValue());
						if (givenName != null)
							contact.setSurname(givenName.getValue());
						for (PhoneNumber phoneNumber : phoneNumbers) {
							String phoneNumberString = phoneNumber
									.getPhoneNumber();
							if (phoneNumberString != null
									&& !phoneNumberString.trim().equals("")) {
								contact.getPhoneNumbers().add(
										phoneNumber.getPhoneNumber());
							}
						}
						this.contacts.add(contact);
					}else {
						System.err.println(name.getFullName());
					}
			
				}
			}
		  }
		  return true;
		
	}

	public void setUsername(String username) {
		this.username = username;
		// TODO Auto-generated method stub
		
	}

	public void setPassword(String password) {
		this.password = password;
		// TODO Auto-generated method stub
		
	}
}
